package com.wjh.base;

import java.io.Serializable;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
 * API接口统一响应参数
 * 
 * @author wenjianhai
 * @date 2023/5/8
 * @since JDK 17
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
public class ResponseApiData<T> implements Serializable {

	private static final long serialVersionUID = 3249106456686948641L;

	private String code;
	private String message;
	private T data;

	private ResponseApiData() {
	}

	public ResponseApiData(ResponseApiCode responseApiCode) {
		this.code = responseApiCode.getCode();
		this.message = responseApiCode.getMessage();
	}

	public static <T> ResponseApiData<T> success(String message, T data) {
		ResponseApiData<T> result = success(data);
		result.setMessage(message);
		return result;
	}

	public static <T> ResponseApiData<T> success(T data) {
		ResponseApiData<T> result = success();
		result.setData(data);
		return result;
	}

	public static <T> ResponseApiData<T> success() {
		return new ResponseApiData(ResponseApiCode.SUCCESS);
	}

	public static <T> ResponseApiData<T> success(String message) {
		ResponseApiData<T> result = success();
		result.setMessage(message);
		return result;
	}

	public static ResponseApiData systemFail(String message) {
		ResponseApiData result = new ResponseApiData(ResponseApiCode.SYSTEM_ERROR);
		result.setMessage(message);
		return result;
	}

	public static ResponseApiData systemFail() {
		ResponseApiData result = new ResponseApiData(ResponseApiCode.SYSTEM_ERROR);
		return result;
	}

	public static ResponseApiData parameterFail(String message) {
		ResponseApiData result = new ResponseApiData(ResponseApiCode.PARAMETER_ERROR);
		result.setMessage(message);
		return result;
	}

	public static ResponseApiData parameterFail() {
		ResponseApiData result = new ResponseApiData(ResponseApiCode.PARAMETER_ERROR);
		return result;
	}

	public static ResponseApiData fail(String code, String message) {
		ResponseApiData result = new ResponseApiData();
		result.setCode(code);
		result.setMessage(message);
		return result;
	}

	public static ResponseApiData fail(String code, String message, String data) {
		ResponseApiData result = new ResponseApiData();
		result.setCode(code);
		result.setMessage(message);
		result.setData(data);
		return result;
	}

	public static ResponseApiData buildResult(ResponseApiCode responseApiCode) {
		return new ResponseApiData(responseApiCode);
	}

	public static <T> ResponseApiData<T> buildResult(ResponseApiCode responseApiCode, T data) {
		ResponseApiData result = new ResponseApiData(responseApiCode);
		result.setData(data);
		return result;
	}
}
